package cn.ekuma.epos.datalogic.define.dao;

import cn.ekuma.data.dao.ModifiedLogDAO;
import cn.ekuma.epos.db.table.I_ProductPrice;
import com.openbravo.bean.Product;
import com.openbravo.bean.ProductPrice;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataParams;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.DataWrite;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.PreparedSentence;
import com.openbravo.data.loader.SerializerWriteParams;
import com.openbravo.data.loader.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;

public class ProductPriceDAO extends ModifiedLogDAO<ProductPrice>{

    public ProductPriceDAO(I_Session s) {
        super(s);
    }
    
    @Override
    public TableDefinition getTable() {
        return new TableDefinition(s,
                "PRODUCT_PRICE",
                new Field[]{
                   new Field(I_ProductPrice.ID,Datas.STRING, Formats.STRING),
                   new Field(I_ProductPrice.PRODUCT,Datas.STRING, Formats.STRING,Product.class),
                   new Field(I_ProductPrice.GROUPNAME,Datas.STRING, Formats.STRING),
                   new Field(I_ProductPrice.PRICESELL, Datas.DOUBLE, Formats.CURRENCY),
                   new Field(I_ProductPrice.CUSTOMERPRICESELL, Datas.DOUBLE, Formats.CURRENCY),
                   new Field(I_ProductPrice.LASTMODIFIED,Datas.TIMESTAMP, Formats.TIMESTAMP)
                } ,
                new int[]{0});
    }

	@Override
	public void writeInsertValues(DataWrite dp, ProductPrice obj)
			throws BasicException {
		dp.setString(1,obj.getId());
		dp.setString(2,obj.getProdcutID());
		dp.setString(3,obj.getGroupName());
		dp.setDouble(4,obj.getM_dPriceSell());
		dp.setDouble(5,obj.getM_dCustomerPrice());
		dp.setTimestamp(6,obj.getLastModified());
		
	}

	@Override
	public Class getSuportClass() {
		return ProductPrice.class;
	}

	@Override
	public ProductPrice readValues(DataRead dr, ProductPrice obj)
			throws BasicException {
	    if(obj==null)
	    	obj=new ProductPrice();
	    obj.setId(dr.getString(1));
	    obj.setProdcutID(dr.getString(2));
	    obj.setGroupName(dr.getString(3));
	    obj.setM_dPriceSell(dr.getDouble(4));
	    obj.setM_dCustomerPrice(dr.getDouble(5));
	    obj.setLastModified(dr.getTimestamp(6));
		return obj;
	}
	
	public ProductPrice findProductPriceByGroupAndProduct(final String productId,final String groupName) throws BasicException{
		return (ProductPrice) new PreparedSentence(s,this.getTableDefinition().getListSQL()+" WHERE PRODUCT=? AND GROUPNAME=? ", SerializerWriteParams.INSTANCE,this
        ).find(new DataParams() {
                public void writeValues() throws BasicException {
                   setString(1, productId);
                   setString(2, groupName);
        }});
	}

}
